package com.jinzhi.common.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Writer;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class QrCodeUtil {
    public static void main(String[] args) {
        int n=20;
        long sum=0;

        for(long i=1,j=0;i<=n;i++){
            if(i==1){
                j=20;
            }else if(i==2){
                j=50;
            }else{
                j=j*2;
            }
            sum+=j;
            System.out.println("第"+i+"天:"+j);

        }
        System.out.println(n+"天共："+sum+"元");
    }

    public static File createQrCode(String url, String path, String fileName) {
        try {
            Map<EncodeHintType, String> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);
            Writer writer = new QRCodeWriter();
            File file = new File(path, fileName);
            if (file.exists() || ((file.getParentFile().exists() || file.getParentFile().mkdirs()) && file.createNewFile())) {
                writeToFile(bitMatrix, "jpg", file);
                log.info("path:"+path);
                log.info("搞定：" + file);
                return file;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static File createQrCodeLogo(String url, String path, String fileName,String logo) {
        if(logo==null){
            return null;
        }
        try {
            int width=400;
            int height=400;
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints);
            BufferedImage bufferedImage = toBufferedImage(bitMatrix);
            Graphics2D gs = bufferedImage.createGraphics();
            File file = new File(path, fileName);
            Image logoImg = ImageIO.read(new File(logo));
            gs.drawImage(logoImg,width/5*2,height/5*2,width/5,height/5,null);
            BasicStroke stroke = new BasicStroke(6,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
            gs.setStroke(stroke);// 设置笔画对象
            //指定弧度的圆角矩形
            RoundRectangle2D.Float round = new RoundRectangle2D.Float(width/5*2-3,height/5*2-3, width/5+6, height/5+6,20,20);
            gs.setColor(Color.white);
            gs.draw(round);// 绘制圆弧矩形



            gs.dispose();
            bufferedImage.flush();
            if (file.exists() || ((file.getParentFile().exists() || file.getParentFile().mkdirs()) && file.createNewFile())) {
               // writeToFile(bitMatrix, "jpg", file);
                log.info("path:"+path);
                log.info("搞定：" + file);
                ImageIO.write(bufferedImage,"jpg",file);
                return file;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, file)) {
            throw new IOException("Could not write an image of format " + format + " to " + file);
        }
    }

    static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        if (!ImageIO.write(image, format, stream)) {
            throw new IOException("Could not write an image of format " + format);
        }
    }

    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    private static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        return image;
    }


}
